<?php
/*
 * 群发审核管理
 * @author wanli
 * @time   2012/10/22  
 */
class Controller_Admin_Fsend extends Controller_Template 
{
	public function __construct ($req, $res)
	{
		$this->template = 'smarty:admin/fsend/default';
		parent::__construct ($req, $res);
		//$this->_merchant = User::current_user();
	}

	public function action_index ()
	{
		$user_cities = User::cities();
	    if($user_cities){
		   $current_city = current($user_cities);
		   $current_city_id = $current_city['id'];
		}
		$url = '/fsend/list/sms/'.$current_city_id.'/1';
        $this->request->redirect($url);
	}
	
	/**
	 * 群发审核记录列表
	 * @param string $type  "sms|email"
	 * @param int    $city_id
	 * @param  int    $page
	 * @return array
	 *
	 */
	public function  action_list($type = 'sms',$city_id,$page = 1 )
	{
		$city_find = $this->city_by_user($city_id);
		if(!$city_find) throw new SCException('参数错误！');
		 
		if(($type != 'sms') && ($type != 'email'))
			throw new SCException('参数错误！');

		$shop_ids = $this->shop_by_city((int)$city_id);
		if(empty($shop_ids)){$shop_ids = 0;}
	
		$query    = array('shop_id' => $shop_ids,'is_batch'=>1,'order_by' => array('send_time'=>'DESC'));

		$limit = kohana::config('client.limit');
		$start = $limit*($page-1);
		$start = ($start<=0) ? 0 : $start;
		if($type == 'sms'){
		    $record_list  = Shop_Relate_Sms::search_send_info($query,$limit,$start);
		}else{
			$record_list  = Shop_Relate_Mail::search_send_info($query,$limit,$start);
		}
		
		$record_count   = intval($record_list['count']);
		$page_total     = ceil($record_count/$limit);
		$page_config    = array(
				'current_page'     => array('source'=> 'route','key'=>'param3'),
				'total_items'      => $record_count,
				'items_per_page'   => $limit,
				// 'view'             => 'pagination/basic',
				'auto_hide'        => true,
				'first_page_in_url'=> true,
		);
		 
		$user_cities = User::cities();
		$pagination = Pagination::factory($page_config);
		$this->template->set('pagination',$pagination->render());
		$this->template->set('user_cities',$user_cities);
		$this->template->set('city_id',$city_id);
		$this->template->set('record_list',$record_list['list']);
		//print_r($record_list);
		$this->template->set('count',$record_count);
		$this->template->set('current_time',time());
		$this->template->set('type',$type);
	}
	
	/**
	 *  记录状态变更
	 *  @param $type    string "sms|email"
	 *  @param $status  string "passed|unpassed"
	 *  @param $task_id int 记录id
	 */
	public function action_fsend_status($type,$status,$task_id){
		$rel = false;
		if($status == 'passed' || $status == 'unpassed'){
		 if($type == 'sms' ) $rel = Shop_Relate_Sms::set_status($task_id,$status);
		 if($type == 'email' ) $rel = Shop_Relate_Mail::set_status($task_id,$status);
		}
		echo $rel ? 1 : 0;
		exit;
	}
	
	/**
	 * 获取邮件详细信息
	 *   
	 */
	public function action_fsend_email($task_id){
		$task_id = (int) $task_id;
		$email_list = Shop_Relate_Mail::get_sent_mail_detail($task_id);
		$file = "smarty:admin/fsend/include/fsend_info";
		$this->template->set('email',$email_list);
		$this->template->set_filename($file);
	}
	/**
	 * 根据$city_id 获取所有开通店铺ids
	 * @return array
	 */
	private function shop_by_city($city_id,$shop_name=null)
	{
		$query     = array('city'=>$city_id,'shop_status'=> "open");
		if($shop_name){
			$query['shop_name'] = $shop_name;
		}
		$shop_info = Shop::search($query);
		$shop_ids = array();
		if($shop_info['list'])
		{
			foreach($shop_info['list'] as $value)
			{
				$shop_ids[] = $value['id'];
			}
		}
		return $shop_ids;
	}
	
	/**
	 *  判定$city_id 是否在用户的编辑权限内
	 */
	private function city_by_user($city_id)
	{
		$user_cities = User::cities();
		$city_find = false;
		foreach($user_cities as $value)
		{
			if($city_id == $value['id'])
			{
				$city_find = true;
				break;
			}
		}
		return $city_find;
	}
	
	/**
	 * 跳转url
	 *
	 */
	private function chg_url()
	{
	
		// 返回页面
		$url       = $_SERVER["HTTP_REFERER"];
		if(!$url){
			$url = '/fsend/index';
		}
		$this->request->redirect($url);
	}
}
